import { asyncRouterMap, constantRouterMap } from '@/config/router.config';
import _ from "lodash";

const top_route = ['/system', '/blank_view', '/workplace', '/userProfile', '/operationPlanManage', '/operationPlanManage/add', '/operationPlanManage/view', '/operationPlanManage/edit', '/operationPlanManage/audit']

/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param {*} authorities
 * @param {*} route
 * @returns {boolean}
 */
function hasAuthority (author, route) {
  if (route.meta) {
    if (route.meta.role) {
      for (let i = 0; i < author.roleList.length; i++) {
        if (route.meta.role.includes(author.roleList[i])) {
          // 用户角色列表包含路由中的role，则允许访问
          return true
        }
      }
    }
    if (route.meta.permission) {
      for (let i = 0; i < author.permissionList.length; i++) {
        // 用户权限列表包含路由中的permission，则允许访问
        if (route.meta.permission.includes(author.permissionList[i])) {
          return true
        }
      }
    }
    // 路有没有设置role和permission，允许访问
    if (!route.meta.role && !route.meta.permission) {
      return true
    }
    // 其他情况，不允许访问
    return false

  } else {

    return true

  }
}

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */

function filterAsyncRouter (routerMap, roles) {
  const accessedRouters = routerMap.filter(route => {
    if (hasAuthority(roles, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true
    }
    return false
  })
  return accessedRouters
}
const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
    userPermission: [],
    userRoles: {}
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    },
    SET_MENUROUTERS: (state, routers) => {
      state.routers = state.routers.map(route => {
        if (route.name === 'index') {
          route.children.splice(1, 0, ...routers)
        }
        return route
      })
    },
    SET_RESETROUTES: (state) => {
      state.addRouters = []
      state.routers = []
      state.userRoles = {}
    },
    SET_USERROLE: (state, roles) => {
      state.userRoles = roles
    }
  },
  actions: {
    GenerateRoutes ({ commit, state }, data) {
      const { roles, menu } = data
      return new Promise(resolve => {
        let rMap = _.cloneDeep(asyncRouterMap);
        let routerView = rMap.map(item => {
          if (item.children && item.children.length) {
            let router = item.children
            return {...item, children: router.filter(route => {
              if (top_route.find(item => item === route.path)){
                return route
              }
            })}
          } else {
            return item
          }
        })
        const accessedRouters = filterAsyncRouter(routerView, roles)
        commit('SET_ROUTERS', accessedRouters)
        commit('SET_MENUROUTERS', menu)
        commit('SET_USERROLE', roles)
        resolve()
      })
    },
    ResetRoutes ({ commit }) {
      commit('SET_RESETROUTES')
    }
  }
}

export default permission
